﻿using Abp.Dependency;
using Abp.Domain.Uow;
using Abp.RemoteEventBus;
using Abp.Runtime.Caching;
using FwProject.Core.Distribution;
using System;
using System.Collections.Generic;
using System.Text;
using Abp.Domain.Repositories;
using FwProject.Core.AppVersion;
using FwProject.Core.Car;
using FwProject.Car;
using FwProject.Core.FactoryInfo;
using FwProject.Core.Restaurant;
using FwProject.Core.SettlementInfoModel;
using FwProject.Core.TypeNumber;
using Castle.Core.Logging;
using Abp.ObjectMapping;
using Abp.ElasticSearch;
using FwProject.BrandInfo;
using System.Threading.Tasks;
using FwProject.BrandInfo.Dto;
using FwProject.ShoppingMall.Shop;
using FwProject.Shop.Dto;
using FwProject.Shop;
using FwProject.Authorization.Users;
using FwProject.ShoppingMall.Com;
using System.Linq;
using FwProject.PublicService.JPush;
using FwProject.Comm;
using FwProject.JPush.JPushModel;

namespace FwProject.ShopApplication.RemoteEventBus
{
    /// <summary> 
    ///发货修改  ShopOrderAppService 在使用
    /// </summary>
    [RemoteEventHandler(ForType = RemoteTypeHelp.TYPEORDERDETAILSDELIVERYSTATUS, ForTopic = RemoteTypeHelp.TOPICAPPORDERDETAILS)]
    public class RemoteEventBusOrderDetails : IRemoteEventHandler, ITransientDependency
    {
        public ICacheManager _catchmanager { get; set; }
        public IElasticsearch _elasticsearch;
        public IBrandInfoAppService _brandInfoAppService;
        public IShopProductAppService _shopProductAppService;
        private readonly IRepository<User, long> _repositoryUser;
        private readonly IRepository<ShopOrderDetails, Guid> _repositoryShopOrderDetails;
        private readonly IRepository<ShopOrder, Guid> _repositoryShopOrder;
        private readonly IJPushAppservice _jPushAppservice;
        public ILogger iLogger { get; set; }

        public IObjectMapper _objectMapper { get; set; }

        public RemoteEventBusOrderDetails(
                IElasticsearch elasticsearch,
                IBrandInfoAppService brandInfoAppService,
                IShopProductAppService shopProductAppService,
                IRepository<User, long> repositoryUser,
                IRepository<ShopOrderDetails, Guid> repositoryShopOrderDetails,
                IRepository<ShopOrder, Guid> repositoryShopOrder,
                IJPushAppservice jPushAppservice
            )
        {
            _jPushAppservice = jPushAppservice;
            _repositoryShopOrder = repositoryShopOrder;
            _repositoryUser = repositoryUser;
            _repositoryShopOrderDetails = repositoryShopOrderDetails;
            _objectMapper = NullObjectMapper.Instance;
            _elasticsearch = elasticsearch;
            _brandInfoAppService = brandInfoAppService;
            _shopProductAppService = shopProductAppService;
        }
            

        [UnitOfWork]
        public void HandleEvent(RemoteEventArgs eventArgs)
        {
            Guid id = Guid.Parse(eventArgs.EventData.Data[RemoteTypeHelp.MODELID].ToString());
            try
            {
                ///如果订单发货明细也统一发货
                var model = _repositoryShopOrderDetails.Get(id);
              
                if (model != null) {

                    var order = _repositoryShopOrder.Get(model.OrderId.Value);
                    var user = _repositoryUser.Get(order.UserId);
              
                    PayNotifyPushMessagModel pushmodel = new PayNotifyPushMessagModel();
                    pushmodel.Id = id;
                    pushmodel.UserId = user.Id;
                    _jPushAppservice.ShopApp(user.Mobile, pushmodel, $"订单编号{order.OrderNumber}已发货请查看详情");
                }

            }
            catch (Exception exe)
            {
                iLogger.Error($"orderid{id}统一发货明细失败", exe);

            }
        }
    }
}
